package com.meta.act.app.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.meta.act.app.system.repository.TenantBaseRepository;
import com.meta.act.app.tenant.entity.TenantBase;
import com.meta.framework.service.ISysLogininforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.meta.act.entity.SysLogininfor;
import com.meta.act.app.system.mapper.SysLogininforMapper;
import org.springframework.util.CollectionUtils;

/**
 * 系统访问日志情况信息 服务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysLogininforServiceImpl implements ISysLogininforService
{

    @Autowired
    private SysLogininforMapper logininforMapper;

    @Autowired
    private TenantBaseRepository tenantBaseRepository;

    /**
     * 新增系统登录日志
     * 
     * @param logininfor 访问日志对象
     */
    @Override
    public void insertLogininfor(SysLogininfor logininfor)
    {
        logininforMapper.insertLogininfor(logininfor);
    }

    /**
     * 查询系统登录日志集合
     * 
     * @param logininfor 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public List<SysLogininfor> selectLogininforList(SysLogininfor logininfor)
    {
        List<SysLogininfor> loginInfoList = logininforMapper.selectLogininforList(logininfor);
        List<TenantBase> tenantList =  tenantBaseRepository.listTenantBase(new TenantBase());

        // 批量根据ids获取租户信息map
        Map<Long, String> tenantInfoMap = new HashMap<>();
        if (!CollectionUtils.isEmpty(tenantList)) {
            for (TenantBase tenantBase : tenantList) {
                tenantInfoMap.put(tenantBase.getTenantId(), tenantBase.getName());
            }
        }

        for(SysLogininfor loginInfo : loginInfoList){
            // 设置租户名称
            loginInfo.setTenantName("未知");
            if(loginInfo.getTenantId() != null){
                String tenantName = tenantInfoMap.get(loginInfo.getTenantId());
                if (null != tenantName) {
                    loginInfo.setTenantName(tenantName);
                }
            }
        }
        return loginInfoList;
    }

    /**
     * 批量删除系统登录日志
     * 
     * @param infoIds 需要删除的登录日志ID
     * @return
     */
    @Override
    public int deleteLogininforByIds(Long[] infoIds)
    {
        return logininforMapper.deleteLogininforByIds(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLogininfor()
    {
        logininforMapper.cleanLogininfor();
    }
}
